package com.mornd.gulimall.common.core.utils;

import com.alibaba.fastjson.JSON;
import com.mornd.gulimall.common.core.result.AjaxResult;
import org.springframework.http.MediaType;

import javax.servlet.ServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;

/**
 * @author: mornd
 * @dateTime: 2023/8/31 - 8:32
 */
public abstract class RespUtil {

    public static void writeUtf8JSON(ServletResponse response, AjaxResult r) throws IOException {
        writeUtf8JSON(response, JSON.toJSONString(r));
    }

    public static void writeUtf8JSON(ServletResponse response, String r) throws IOException {
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        PrintWriter writer = response.getWriter();
        writer.write(r);
        writer.flush();
        writer.close();
    }
}
